﻿using Eu.Xquirrel.Networking.Http.Offline;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace TestApplication
{
    /// <summary>
    /// Other than the WebView, and the INotifyPropertyChanged implementation, this is a standard page.
    /// The INotifyPropertyChanged interface is implemented at the bottom of the page and it's
    /// reason for existing is to cause the WebView to bind to the Url that is passed to the
    /// Offline Http Server when the WebView automatically tries to connect with it.
    /// </summary>
    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        private int _designatedPort = 7777;
        private string _designatedFolderPathInPicturesFolderAsHttpRoot;
        public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = this;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // NOTE: I've hard-coded these values here but in an application they would typically be parsed
            // from the NavigationEventArgs so that the content of different folders would be served as discreet
            // packages on their own. A dynamically navigated content tree in Pictures would typically
            // have been created by the same application that downloaded ZIP files from the web and exploded
            // them into the Pictures folder, hence the application should be able to navigate from
            // one to the other based upon the user choosing which he or she wanted to view at any
            // particular point in time.
            //
            // Also, please take special note that both the Source in the xaml declaration of the WebView,
            // and the Navigate method in the code-behind BOTH need to be populated with the same data, hence the 
            // binding (utilising the INotifyPropertyChanged nterface) is necessary to dynamically 
            // coordinatee both values. 
            //
            // I haven't figured out why both are needed but necessity is the mother of invention :D

            // Also, see the capabilities declarations in Package.appxmanifest !!!

            _designatedFolderPathInPicturesFolderAsHttpRoot = "html5Content";

            // This assumes that the entry point to the downloaded Zip is index.html 
            SourceUri = "http://localhost:" + _designatedPort.ToString() + "/index.html";

            // in a using statement so that when navigating away from this page that IDispose is called.
            using (OfflineHttpServer server = new OfflineHttpServer(_designatedFolderPathInPicturesFolderAsHttpRoot, _designatedPort))
            {
                mywv.Navigate(new Uri(_sourceUri, UriKind.Absolute));
            }
        }

        /// <summary>
        /// The binding for the Source Uri.
        /// </summary>
        private string _sourceUri;
        public string SourceUri
        {
            get
            {
                return _sourceUri;
            }
            set
            {
                _sourceUri = value;
                // This tells the UI that the value has changed ~ and so the UI asks for the value ...
                // automated binding, as such.
                RaisePropertyChanged("SourceUri");
            }

        }

        // The implementation of the INotifyPropertyChanged interface that we added to the top of the page.
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
